<?php


namespace app\console\controller;


use app\BaseController;


use app\console\service\goods\GoodsQueryService;
use app\console\service\goods\GoodsService;
use app\console\validate\GoodsValidate;
use app\model\Tag;
use app\Request;
use think\App;

function goods2dict($list)
{
    $dateTime = app('DateTime');

    return [
        'id' => $list['id'],
        'supplier_id' => $list['supplier_id'],
        'name' => $list['name'],
        'number' => $list['number'],
        'type' => $list['type'],
        'tags' => json_decode($list['tags'], true),
        'is_packaged' => $list['is_packaged'],
        'material' => $list['material'],
        'suitable_people' => $list['suitable_people'],
        'has_storage' => $list['has_storage'],
        'customizable' => $list['customizable'],
        'model' => json_decode($list['model'], true),
        'other_features' => json_decode($list['other_features'], true),
        'finish_meterial' => $list['finish_meterial'],
        'style' => $list['style'],
        'dispatch_place_province' => $list['dispatch_place_province'],
        'dispatch_place_city' => $list['dispatch_place_city'],
        'dispatch_place_district' => $list['dispatch_place_district'],
        'dispatch_place_address' => $list['dispatch_place_address'],
        'skus' => json_decode($list['skus'], true),
        'machining_scheme_id' => $list['machining_scheme_id'],
        'attrs' => json_decode($list['attrs'], true),
        'invoice' => $list['invoice'],
        'guarantee' => $list['guarantee'],
        'return_promise' => $list['return_promise'],
        'brand' => $list['brand'],
        'detail' => $list['detail'],
        'fill' => $list['fill'],
        'remark' => $list['remark'],
        'pictures' => json_decode($list['pictures'], true),
        'images' => json_decode($list['images'], true),
        'status' => $list['status'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'format_create_time' => $dateTime->timestamp2date($list['create_time']),
        'format_update_time' => $dateTime->timestamp2date($list['update_time']),
    ];
}

//标签
class GoodsController extends BaseController
{

    public function create(Request $request, GoodsService $goodsService)
    {
        $param = $request->param([
            'supplier_id', 'name', 'number',
            'type', 'tags',
            'is_packaged', 'material', 'suitable_people',
            'has_storage', 'customizable', 'model',
            'fill', 'other_features', 'finish_meterial',
            'style', 'dispatch_place_province', 'dispatch_place_city',
            'dispatch_place_district', 'dispatch_place_address', 'skus',
            'machining_scheme_id', 'attrs', 'invoice',
            'guarantee', 'return_promise', 'brand',
            'detail', 'remark'
        ]);

        $param['pictures'] = $request->file('pictures');
        $param['images'] = $request->file('images');

        validate(GoodsValidate::class)
            ->scene('create')
            ->check($param);

        $goods = $goodsService->create($param);

        return $this->returnJson(goods2dict($goods));

    }

    public function get(Request $request, GoodsQueryService $goodsQueryService){
        $all = $request->param('all', 0);
        $page = $request->param('page', 1);
        $perPage = $request->param('perPage', config('app.allow_limit'));
        $name = $request->param('name');
        $number = $request->param('number');
        $status = $request->param('status');
        $search_start_time = $request->get('start_time');
        $search_end_time = $request->get('end_time');

        if (!$all) {
            validate(GoodsValidate::class)
                ->scene('getPage')
                ->check([
                    'page'         => $page ,
                    'perPage'      => $perPage ,
                    'search_start_time'   => $search_start_time ,
                    'search_end_time'     => $search_end_time ,
                    'status'     => $status ,
                ]);

            $goods = $goodsQueryService->getPageList([
                'page'     =>  $page,
                'perPage'  =>  $perPage,
                'name'  =>  $name,
                'number' =>  $number,
                'status'     =>  $status,
                'search_start_time'  =>  $search_start_time,
                'search_end_time'    =>  $search_end_time
            ]);
        }else{
            validate(GoodsValidate::class)
                ->scene('getAll')
                ->check([
                    'search_start_time'   => $search_start_time ,
                    'search_end_time'     => $search_end_time ,
                    'status'     => $status ,
                ]);

            $goods = $goodsQueryService->getList([
                'name'  =>  $name,
                'number' =>  $number,
                'status'     =>  $status,
                'search_start_time'  =>  $search_start_time,
                'search_end_time'    =>  $search_end_time
            ]);
        }

        return $this->returnList(
            array_map(fn($list) => goods2dict($list), $goods['list']->toArray()),
            $goods['pagination']['page'], $goods['pagination']['perPage'], $goods['pagination']['total']
        );
    }
//
//    public function update(Request $request, TagService $tagService){
//        $name = $request->param('name', '');
//
//        if (empty($name)) {
//            throw new ParamException('未修改任何数据');
//        }
//
//        $tag = $tagService->update($name);
//
//        return $this->returnJson(tag2dict($tag));
//    }
//
//    public function delete(TagService $tagService){
//        $tagService->delete();
//
//        return $this->returnJson();
//    }
//
//    public function getById(Tag $tag){
//        return $this->returnJson(tag2dict($tag));
//    }

}